/*
 * @(#)OperateNode.java   09/12/09
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



/**
 *
 */
package fr.ema.arch3d.jmodel.transformXML;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * class description
 *
 * @version    version 1 nov. 2009
 * @author     Huaxi (Yulin) ZHANG
 */
public class OperateNode {

    /**
     * Method description
     *
     *
     * @param uri
     * @param addUri
     */
    static public void addIncludeNode(String uri, String addUri) {
        try {
            File file = new File(uri);

            if (file.exists()) {
                DocumentBuilderFactory factory  = DocumentBuilderFactory.newInstance();
                DocumentBuilder        builder  = factory.newDocumentBuilder();
                Document               doc      = builder.parse(file);
                TransformerFactory     tFactory = TransformerFactory.newInstance();
                Transformer            tFormer  = tFactory.newTransformer();
                Element                element  = doc.createElement("xinclude:include");

                element.setAttribute("href", addUri);
                element.setAttribute("parse", "xml");
                doc.getElementsByTagName("Specification").item(0).appendChild(element);

                // Normalize the DOM tree to combine all adjacent nodes
                doc.normalize();

                Source source = new DOMSource(doc);
                Result dest   = new StreamResult(file);

                tFormer.transform(source, dest);
            } else {
                System.out.println("File not found!");
            }
        } catch (Exception e) {
            System.err.println(e);
            System.exit(0);
        }
    }

    /**
     * Method description
     *
     *
     * @param uri
     * @param remElement
     */
    static public void removeNode(String uri, String remElement) {
        try {
            File file = new File(uri);

            if (file.exists()) {
                DocumentBuilderFactory factory  = DocumentBuilderFactory.newInstance();
                DocumentBuilder        builder  = factory.newDocumentBuilder();
                Document               doc      = builder.parse(file);
                TransformerFactory     tFactory = TransformerFactory.newInstance();
                Transformer            tFormer  = tFactory.newTransformer();
                Element                element  = (Element) doc.getElementsByTagName(remElement).item(0);

                // Remove the node
                element.getParentNode().removeChild(element);

                // Normalize the DOM tree to combine all adjacent nodes
                doc.normalize();

                Source source = new DOMSource(doc);
                Result dest   = new StreamResult(file);

                tFormer.transform(source, dest);
            } else {
                System.out.println("File not found!");
            }
        } catch (Exception e) {
            System.err.println(e);
            System.exit(0);
        }
    }
}
